home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / fileio.c < prev    next >
C/C++ Source or Header  |  1992-05-23  |  66KB  |  2,478 lines

  1. /* File IO for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <sys/types.h>
  22. #ifdef hpux
  23. /* needed by <pwd.h> */
  24. #include <stdio.h>
  25. #undef NULL
  26. #endif
  27. #include <sys/stat.h>
  28.  
  29. #ifdef VMS
  30. #include "pwd.h"
  31. #else
  32. #include <pwd.h>
  33. #endif
  34.  
  35. #include <ctype.h>
  36.  
  37. #ifdef VMS
  38. #include "dir.h"
  39. #include <perror.h>
  40. #include <stddef.h>
  41. #include <string.h>
  42. #else
  43. #include <sys/dir.h>
  44. #endif
  45. #include <errno.h>
  46.  
  47. #ifndef vax11c
  48. extern int errno;
  49. extern char *sys_errlist[];
  50. extern int sys_nerr;
  51. #endif
  52.  
  53. #define err_str(a) ((a) < sys_nerr ? sys_errlist[a] : "unknown error")
  54.  
  55. #ifdef APOLLO
  56. #include <sys/time.h>
  57. #endif
  58.  
  59. #ifdef NULL
  60. #undef NULL
  61. #endif
  62. #include "config.h"
  63. #include "lisp.h"
  64. #include "buffer.h"
  65. #include "window.h"
  66.  
  67. #ifdef VMS
  68. #include <file.h>
  69. #include <rmsdef.h>
  70. #include <fab.h>
  71. #include <nam.h>
  72. extern unsigned char vms_file_written[];    /* set in rename_sans_version */
  73. #endif
  74.  
  75. #ifdef HAVE_TIMEVAL
  76. #ifdef HPUX
  77. #include <time.h>
  78. #else
  79. #include <sys/time.h>
  80. #endif
  81. #endif
  82.  
  83. #ifdef HPUX_NET
  84. #include <netio.h>
  85. #ifndef HPUX8
  86. #include <errnet.h>
  87. #endif
  88. #endif
  89.  
  90. #ifndef O_WRONLY
  91. #define O_WRONLY 1
  92. #endif
  93.  
  94. #define min(a, b) ((a) < (b) ? (a) : (b))
  95. #define max(a, b) ((a) > (b) ? (a) : (b))
  96.  
  97. /* Nonzero during writing of auto-save files */
  98. int auto_saving;
  99.  
  100. /* Nonzero means, when reading a filename in the minibuffer,
  101.  start out by inserting the default directory into the minibuffer. */
  102. int insert_default_directory;
  103.  
  104. /* On VMS, nonzero means write new files with record format stmlf.
  105.    Zero means use var format.  */
  106. int vms_stmlf_recfm;
  107.  
  108. Lisp_Object Qfile_error, Qfile_already_exists;
  109.  
  110. report_file_error (string, data)
  111.      char *string;
  112.      Lisp_Object data;
  113. {
  114.   Lisp_Object errstring;
  115.  
  116.   if (errno >= 0 && errno < sys_nerr)
  117.     errstring = build_string (sys_errlist[errno]);
  118.   else
  119.     errstring = build_string ("undocumented error code");
  120.  
  121.   /* System error messages are capitalized.  Downcase the initial
  122.      unless it is followed by a slash.  */
  123.   if (XSTRING (errstring)->data[1] != '/')
  124.     XSTRING (errstring)->data[0] = DOWNCASE (XSTRING (errstring)->data[0]);
  125.  
  126.   while (1)
  127.     Fsignal (Qfile_error,
  128.          Fcons (build_string (string), Fcons (errstring, data)));
  129. }
  130.  
  131. DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory,
  132.   1, 1, 0,
  133.   "Return the directory component in file name NAME.\n\
  134. Return nil if NAME does not include a directory.\n\
  135. Otherwise returns a directory spec.\n\
  136. Given a Unix syntax file name, returns a string ending in slash;\n\
  137. on VMS, perhaps instead a string ending in :, ] or >.")
  138.   (file)
  139.      Lisp_Object file;
  140. {
  141.   register unsigned char *beg;
  142.   register unsigned char *p;
  143.  
  144.   CHECK_STRING (file, 0);
  145.  
  146.   beg = XSTRING (file)->data;
  147.   p = beg + XSTRING (file)->size;
  148.  
  149.   while (p != beg && p[-1] != '/'
  150. #ifdef    AMIGA
  151.          && p[-1] != ':'
  152. #endif    /* AMIGA */
  153. #ifdef VMS
  154.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  155. #endif /* VMS */
  156.      ) p--;
  157.  
  158.   if (p == beg)
  159.     return Qnil;
  160.   return make_string (beg, p - beg);
  161. }
  162.  
  163. DEFUN ("file-name-nondirectory", Ffile_name_nondirectory, Sfile_name_nondirectory,
  164.   1, 1, 0,
  165.   "Return file name NAME sans its directory.\n\
  166. For example, in a Unix-syntax file name,\n\
  167. this is everything after the last slash,\n\
  168. or the entire name if it contains no slash.")
  169.   (file)
  170.      Lisp_Object file;
  171. {
  172.   register unsigned char *beg, *p, *end;
  173.  
  174.   CHECK_STRING (file, 0);
  175.  
  176.   beg = XSTRING (file)->data;
  177.   end = p = beg + XSTRING (file)->size;
  178.  
  179.   while (p != beg && p[-1] != '/'
  180. #ifdef    AMIGA
  181.          && p[-1] != ':'
  182. #endif    /* AMIGA */
  183. #ifdef VMS
  184.      && p[-1] != ':' && p[-1] != ']' && p[-1] != '>'
  185. #endif /* VMS */
  186.      ) p--;
  187.  
  188.   return make_string (p, end - p);
  189. }
  190.  
  191. char *
  192. file_name_as_directory (out, in)
  193.      char *out, *in;
  194. {
  195.   int size = strlen (in) - 1;
  196.  
  197.   strcpy (out, in);
  198.  
  199. #ifdef VMS
  200.   /* Is it already a directory string? */
  201.   if (in[size] == ':' || in[size] == ']' || in[size] == '>')
  202.     return out;
  203.   /* Is it a VMS directory file name?  If so, hack VMS syntax.  */
  204.   else if (! index (in, '/')
  205.        && ((size > 3 && ! strcmp (&in[size - 3], ".DIR"))
  206.            || (size > 3 && ! strcmp (&in[size - 3], ".dir"))
  207.            || (size > 5 && (! strncmp (&in[size - 5], ".DIR", 4)
  208.                 || ! strncmp (&in[size - 5], ".dir", 4))
  209.            && (in[size - 1] == '.' || in[size - 1] == ';')
  210.            && in[size] == '1')))
  211.     {
  212.       register char *p, *dot;
  213.       char brack;
  214.  
  215.       /* x.dir -> [.x]
  216.      dir:x.dir --> dir:[x]
  217.      dir:[x]y.dir --> dir:[x.y] */
  218.       p = in + size;
  219.       while (p != in && *p != ':' && *p != '>' && *p != ']') p--;
  220.       if (p != in)
  221.     {
  222.       strncpy (out, in, p - in);
  223.       out[p - in] = '\0';
  224.       if (*p == ':')
  225.         {
  226.           brack = ']';
  227.           strcat (out, ":[");
  228.         }
  229.       else
  230.         {
  231.           brack = *p;
  232.           strcat (out, ".");
  233.         }
  234.       p++;
  235.     }
  236.       else
  237.     {
  238.       brack = ']';
  239.       strcpy (out, "[.");
  240.     }
  241.       dot = (char *) index (p, '.');
  242.       if (dot)
  243.     {
  244.       /* blindly remove any extension */
  245.       size = strlen (out) + (dot - p);
  246.       strncat (out, p, dot - p);
  247.     }
  248.       else
  249.     {
  250.       strcat (out, p);
  251.       size = strlen (out);
  252.     }
  253.       out[size++] = brack;
  254.       out[size] = '\0';
  255.     }
  256. #else /* not VMS */
  257. #ifdef    AMIGA
  258.   /* AmigaDOS syntax, append slash if the last char isn't a ':' or '/' */
  259.   if (out[size] != '/' && out[size] != ':' && size != 0)
  260.     strcat (out, "/");
  261. #else    /* not AMIGA */
  262.   /* For Unix syntax, Append a slash if necessary */
  263.   if (out[size] != '/')
  264.     strcat (out, "/");
  265. #endif /* not AMIGA */
  266. #endif /* not VMS */
  267.   return out;
  268. }
  269.  
  270. DEFUN ("file-name-as-directory", Ffile_name_as_directory,
  271.        Sfile_name_as_directory, 1, 1, 0,
  272.   "Return a string representing file FILENAME interpreted as a directory.\n\
  273. This string can be used as the value of default-directory\n\
  274. or passed as second argument to expand-file-name.\n\
  275. For a Unix-syntax file name, just appends a slash.\n\
  276. On VMS, converts \"[X]FOO.DIR\" to \"[X.FOO]\", etc.")
  277.   (file)
  278.      Lisp_Object file;
  279. {
  280.   char *buf;
  281.  
  282.   CHECK_STRING (file, 0);
  283.   if (NULL (file))
  284.     return Qnil;
  285.   buf = (char *) alloca (XSTRING (file)->size + 10);
  286.   return build_string (file_name_as_directory (buf, XSTRING (file)->data));
  287. }
  288.  
  289. /*
  290.  * Convert from directory name to filename.
  291.  * On VMS:
  292.  *       xyzzy:[mukesh.emacs] => xyzzy:[mukesh]emacs.dir.1
  293.  *       xyzzy:[mukesh] => xyzzy:[000000]mukesh.dir.1
  294.  * On UNIX, it's simple: just make sure there is a terminating /
  295.  
  296.  * Value is nonzero if the string output is different from the input.
  297.  */
  298.  
  299. directory_file_name (src, dst)
  300.      char *src, *dst;
  301. {
  302.   long slen;
  303. #ifdef VMS
  304.   long rlen;
  305.   char * ptr, * rptr;
  306.   char bracket;
  307.   struct FAB fab = cc$rms_fab;
  308.   struct NAM nam = cc$rms_nam;
  309.   char esa[NAM$C_MAXRSS];
  310. #endif /* VMS */
  311.  
  312.   slen = strlen (src) - 1;
  313. #ifdef VMS
  314.   if (! index (src, '/')
  315.       && (src[slen] == ']' || src[slen] == ':' || src[slen] == '>'))
  316.     {
  317.       /* VMS style - convert [x.y.z] to [x.y]z, [x] to [000000]x */
  318.       fab.fab$l_fna = src;
  319.       fab.fab$b_fns = slen + 1;
  320.       fab.fab$l_nam = &nam;
  321.       fab.fab$l_fop = FAB$M_NAM;
  322.  
  323.       nam.nam$l_esa = esa;
  324.       nam.nam$b_ess = sizeof esa;
  325.       nam.nam$b_nop |= NAM$M_SYNCHK;
  326.  
  327.       /* We call SYS$PARSE to handle such things as [--] for us. */
  328.       if (SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL)
  329.     {
  330.       slen = nam.nam$b_esl - 1;
  331.       if (esa[slen] == ';' && esa[slen - 1] == '.')
  332.         slen -= 2;
  333.       esa[slen + 1] = '\0';
  334.       src = esa;
  335.     }
  336.       if (src[slen] != ']' && src[slen] != '>')
  337.     {
  338.       /* what about when we have logical_name:???? */
  339.       if (src[slen] == ':')
  340.         {            /* Xlate logical name and see what we get */
  341.           ptr = strcpy (dst, src); /* upper case for getenv */
  342.           while (*ptr)
  343.         {
  344.           if ('a' <= *ptr && *ptr <= 'z')
  345.             *ptr -= 040;
  346.           ptr++;
  347.         }
  348.           dst[slen] = 0;    /* remove colon */
  349.           if (!(src = egetenv (dst)))
  350.         return 0;
  351.           /* should we jump to the beginning of this procedure?
  352.          Good points: allows us to use logical names that xlate
  353.          to Unix names,
  354.          Bad points: can be a problem if we just translated to a device
  355.          name...
  356.          For now, I'll punt and always expect VMS names, and hope for
  357.          the best! */
  358.           slen = strlen (src) - 1;
  359.           if (src[slen] != ']' && src[slen] != '>')
  360.         { /* no recursion here! */
  361.           strcpy (dst, src);
  362.           return 0;
  363.         }
  364.         }
  365.       else
  366.         {        /* not a directory spec */
  367.           strcpy (dst, src);
  368.           return 0;
  369.         }
  370.     }
  371.       bracket = src[slen];
  372.       ptr = (char *) index (src, bracket - 2);
  373.       if (ptr == 0)
  374.     { /* no opening bracket */
  375.       strcpy (dst, src);
  376.       return 0;
  377.     }
  378.       if (!(rptr = (char *) rindex (src, '.')))
  379.     rptr = ptr;
  380.       slen = rptr - src;
  381.       strncpy (dst, src, slen);
  382.       dst[slen] = '\0';
  383.       if (*rptr == '.')
  384.     {
  385.       dst[slen++] = bracket;
  386.       dst[slen] = '\0';
  387.     }
  388.       else
  389.     {
  390.       /* If we have the top-level of a rooted directory (i.e. xx:[000000]),
  391.          then translate the device and recurse. */
  392.       if (dst[slen - 1] == ':'
  393.           && dst[slen - 2] != ':'    /* skip decnet nodes */
  394.           && strcmp(src + slen, "[000000]") == 0)
  395.         {
  396.           dst[slen - 1] = '\0';
  397.           if ((ptr = egetenv (dst))
  398.           && (rlen = strlen (ptr) - 1) > 0
  399.           && (ptr[rlen] == ']' || ptr[rlen] == '>')
  400.           && ptr[rlen - 1] == '.')
  401.         {
  402.           ptr[rlen - 1] = ']';
  403.           ptr[rlen] = '\0';
  404.           return directory_file_name (ptr, dst);
  405.         }
  406.           else
  407.         dst[slen - 1] = ':';
  408.         }
  409.       strcat (dst, "[000000]");
  410.       slen += 8;
  411.     }
  412.       rptr++;
  413.       rlen = strlen (rptr) - 1;
  414.       strncat (dst, rptr, rlen);
  415.       dst[slen + rlen] = '\0';
  416.       strcat (dst, ".DIR.1");
  417.       return 1;
  418.     }
  419. #endif /* VMS */
  420.   /* Process as Unix format: just remove any final slash.
  421.      But leave "/" unchanged; do not change it to "".  */
  422.   strcpy (dst, src);
  423.   if (dst[slen] == '/' && slen > 0)
  424.     dst[slen] = 0;
  425.   return 1;
  426. }
  427.  
  428. DEFUN ("directory-file-name", Fdirectory_file_name, Sdirectory_file_name,
  429.   1, 1, 0,
  430.   "Returns the file name of the directory named DIR.\n\
  431. This is the name of the file that holds the data for the directory DIR.\n\
  432. In Unix-syntax, this just removes the final slash.\n\
  433. On VMS, given a VMS-syntax directory name such as \"[X.Y]\",\n\
  434. returns a file name such as \"[X]Y.DIR.1\".")
  435.   (directory)
  436.      Lisp_Object directory;
  437. {
  438.   char *buf;
  439.  
  440.   CHECK_STRING (directory, 0);
  441.  
  442.   if (NULL (directory))
  443.     return Qnil;
  444. #ifdef VMS
  445.   /* 20 extra chars is insufficient for VMS, since we might perform a
  446.      logical name translation. an equivalence string can be up to 255
  447.      chars long, so grab that much extra space...  - sss */
  448.   buf = (char *) alloca (XSTRING (directory)->size + 20 + 255);
  449. #else
  450.   buf = (char *) alloca (XSTRING (directory)->size + 20);
  451. #endif
  452.   directory_file_name (XSTRING (directory)->data, buf);
  453.   return build_string (buf);
  454. }
  455.  
  456. DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0,
  457.   "Generate temporary name (string) starting with PREFIX (a string).")
  458.   (prefix)
  459.      Lisp_Object prefix;
  460. {
  461.   Lisp_Object val;
  462.   val = concat2 (prefix, build_string ("XXXXXX"));
  463.   mktemp (XSTRING (val)->data);
  464.   return val;
  465. }
  466.  
  467. DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
  468.   "Convert FILENAME to absolute, and canonicalize it.\n\
  469. Second arg DEFAULT is directory to start with if FILENAME is relative\n\
  470.  (does not start with slash); if DEFAULT is nil or missing,\n\
  471. the current buffer's value of default-directory is used.\n\
  472. Filenames containing . or .. as components are simplified;\n\
  473. initial ~ is expanded.  See also the function  substitute-in-file-name.")
  474.      (name, defalt)
  475.      Lisp_Object name, defalt;
  476. {
  477. #ifdef AMIGA
  478.   unsigned char *nm, *tilde, *newdir, *colon, *t_pos, *target;
  479.  
  480.   CHECK_STRING (name, 0);
  481.  
  482.   nm = XSTRING (name)->data;
  483.   /* Find base directory */
  484.   if (NULL (defalt))
  485.       defalt = current_buffer->directory;
  486.   CHECK_STRING (defalt, 1);
  487.   newdir = XSTRING (defalt)->data;
  488.  
  489.   /* Concat newdir w/ nm and canonicalize */
  490.   /* newdir always contains at least the device name.
  491.      It is assumed canonical */
  492.   target = (unsigned char *)alloca(strlen(nm) + strlen(newdir) + 2);
  493.   file_name_as_directory (target, newdir);
  494.   t_pos = target + strlen(target);
  495.  
  496.   while (*nm)
  497.   {
  498.       unsigned char *comp_end = nm;
  499.       int comp_len;
  500.  
  501.       /* Find next component of path (everything upto the next /) */
  502.       do comp_end++; while (comp_end[0] && comp_end[-1] != '/' && comp_end[-1] != ':');
  503.       comp_len = comp_end - nm;
  504.  
  505.       if (comp_len == 1 && nm[0] == '/' ||
  506.       nm[0] == '.' && nm[1] == '.' &&
  507.       (comp_len == 2 || comp_len == 3 && nm[2] == '/'))
  508.       {
  509.       /* Previous directory */
  510.       if (t_pos > target && t_pos[-1] != ':')
  511.       {
  512.           t_pos--; /* Back up over / */
  513.           while (t_pos > target &&
  514.              t_pos[-1] != ':' && t_pos[-1] != '/') t_pos--;
  515.       }
  516.       }
  517.       else if (comp_len == 2 && nm[0] == '.' && nm[1] == '/' ||
  518.            comp_len == 1 && nm[0] == '.') ; /* Ignore . */
  519.       else if (nm[0] == ':') /* Just keep disk name */
  520.       {
  521.       char *new_pos;
  522.  
  523.       *t_pos = 0; /* Limit search for : */
  524.       t_pos = index(target, ':');
  525.       if (t_pos) t_pos++;
  526.       else t_pos = target;
  527.       }
  528.       else if (nm[0] == '~' || index(nm, ':'))
  529.       {
  530.       char *exp_name;
  531.  
  532.       if (nm[0] == '~')
  533.           if (nm[1] == '/' || nm[1] == 0) /* Home directory */
  534.           {
  535.           newdir = (unsigned char *) egetenv ("HOME");
  536.           if (!newdir) newdir = (unsigned char *) "s:";
  537.           }
  538.           else
  539.           {
  540.           /* Handle ~ followed by user name.  */
  541.           char lastc = nm[comp_len - 1];
  542.           int len = comp_len - 1;
  543.  
  544.           if (lastc == ':' || lastc == '/') len--;
  545.  
  546.           /* ~name becomes name: */
  547.           newdir = (unsigned char *) alloca (len + 2);
  548.           bcopy((char *) nm + 1, newdir, len);
  549.           newdir[len] = ':';
  550.           newdir[len + 1] = 0;
  551.           }
  552.       else /* we have name: */
  553.       {
  554.           newdir = (char *)alloca(comp_len + 1);
  555.           bcopy(nm, newdir, comp_len);
  556.           newdir[comp_len] = 0;
  557.       }
  558.       exp_name = (char *)alloca(1024);
  559.       if (expand_path(newdir, exp_name, 1024))
  560.           if (strncmp(exp_name, "Tao:", 4) != 0) /* Hack for WShell PATH: */
  561.           newdir = exp_name;
  562.       target = (unsigned char *)alloca(strlen(nm) + strlen(newdir) + 2);
  563.       file_name_as_directory (target, newdir);
  564.       t_pos = target + strlen(target);
  565.       }
  566.       else /* Copy component */
  567.       {
  568.       bcopy(nm, t_pos, comp_len);
  569.       t_pos += comp_len;
  570.       }
  571.  
  572.       nm = comp_end;
  573.   }
  574.   return make_string (target, t_pos - target);
  575. #else /* not AMIGA */
  576.   unsigned char *nm;
  577.   
  578.   register unsigned char *newdir, *p, *o;
  579.   int tlen;
  580.   unsigned char *target;
  581.   struct passwd *pw;
  582.   int lose;
  583. #ifdef VMS
  584.   unsigned char * colon = 0;
  585.   unsigned char * close = 0;
  586.   unsigned char * slash = 0;
  587.   unsigned char * brack = 0;
  588.   int lbrack = 0, rbrack = 0;
  589.   int dots = 0;
  590. #endif /* VMS */
  591.   
  592.   CHECK_STRING (name, 0);
  593.  
  594. #ifdef VMS
  595.   /* Filenames on VMS are always upper case.  */
  596.   name = Fupcase (name);
  597. #endif
  598.  
  599.   nm = XSTRING (name)->data;
  600.   
  601.   /* If nm is absolute, flush ...// and detect /./ and /../.
  602.      If no /./ or /../ we can return right away. */
  603.   if (
  604.       nm[0] == '/'
  605. #ifdef VMS
  606.       || index (nm, ':')
  607. #endif /* VMS */
  608.       )
  609.     {
  610.       p = nm;
  611.       lose = 0;
  612.       while (*p)
  613.     {
  614.       if (p[0] == '/' && p[1] == '/'
  615. #ifdef APOLLO
  616.           /* // at start of filename is meaningful on Apollo system */
  617.           && nm != p
  618. #endif /* APOLLO */
  619.           )
  620.         nm = p + 1;
  621.       if (p[0] == '/' && p[1] == '~')
  622.         nm = p + 1, lose = 1;
  623.       if (p[0] == '/' && p[1] == '.'
  624.           && (p[2] == '/' || p[2] == 0
  625.           || (p[2] == '.' && (p[3] == '/' || p[3] == 0))))
  626.         lose = 1;
  627. #ifdef VMS
  628.       if (p[0] == '\\')
  629.         lose = 1;
  630.       if (p[0] == '/') {
  631.         /* if dev:[dir]/, move nm to / */
  632.         if (!slash && p > nm && (brack || colon)) {
  633.           nm = (brack ? brack + 1 : colon + 1);
  634.           lbrack = rbrack = 0;
  635.           brack = 0;
  636.           colon = 0;
  637.         }
  638.         slash = p;
  639.       }
  640.       if (p[0] == '-')
  641. #ifndef VMS4_4
  642.         /* VMS pre V4.4,convert '-'s in filenames. */
  643.         if (lbrack == rbrack)
  644.           {
  645.         if (dots < 2)    /* this is to allow negative version numbers */
  646.           p[0] = '_';
  647.           }
  648.         else
  649. #endif /* VMS4_4 */
  650.           if (lbrack > rbrack &&
  651.           ((p[-1] == '.' || p[-1] == '[' || p[-1] == '<') &&
  652.            (p[1] == '.' || p[1] == ']' || p[1] == '>')))
  653.         lose = 1;
  654. #ifndef VMS4_4
  655.           else
  656.         p[0] = '_';
  657. #endif /* VMS4_4 */
  658.       /* count open brackets, reset close bracket pointer */
  659.       if (p[0] == '[' || p[0] == '<')
  660.         lbrack++, brack = 0;
  661.       /* count close brackets, set close bracket pointer */
  662.       if (p[0] == ']' || p[0] == '>')
  663.         rbrack++, brack = p;
  664.       /* detect ][ or >< */
  665.       if ((p[0] == ']' || p[0] == '>') && (p[1] == '[' || p[1] == '<'))
  666.         lose = 1;
  667.       if ((p[0] == ':' || p[0] == ']' || p[0] == '>') && p[1] == '~')
  668.         nm = p + 1, lose = 1;
  669.       if (p[0] == ':' && (colon || slash))
  670.         /* if dev1:[dir]dev2:, move nm to dev2: */
  671.         if (brack)
  672.           {
  673.         nm = brack + 1;
  674.         brack = 0;
  675.           }
  676.         /* if /pathname/dev:, move nm to dev: */
  677.         else if (slash)
  678.           nm = slash + 1;
  679.         /* if node::dev:, move colon following dev */
  680.         else if (colon && colon[-1] == ':')
  681.           colon = p;
  682.         /* if dev1:dev2:, move nm to dev2: */
  683.         else if (colon && colon[-1] != ':')
  684.           {
  685.         nm = colon + 1;
  686.         colon = 0;
  687.           }
  688.       if (p[0] == ':' && !colon)
  689.         {
  690.           if (p[1] == ':')
  691.         p++;
  692.           colon = p;
  693.         }
  694.       if (lbrack == rbrack)
  695.         if (p[0] == ';')
  696.           dots = 2;
  697.         else if (p[0] == '.')
  698.           dots++;
  699. #endif /* VMS */
  700.       p++;
  701.     }
  702.       if (!lose)
  703.     {
  704. #ifdef VMS
  705.       if (index (nm, '/'))
  706.         return build_string (sys_translate_unix (nm));
  707. #endif /* VMS */
  708.       if (nm == XSTRING (name)->data)
  709.         return name;
  710.       return build_string (nm);
  711.     }
  712.     }
  713.  
  714.   /* Now determine directory to start with and put it in NEWDIR.  */
  715.  
  716.   newdir = 0;
  717.  
  718.   if (nm[0] == '~')
  719.     {
  720.       if (nm[1] == '/'
  721. #ifdef VMS
  722.       || nm[1] == ':'
  723. #endif /* VMS */
  724.       || nm[1] == 0)
  725.     {
  726.       /* Handle ~ on its own.  */
  727.       newdir = (unsigned char *) egetenv ("HOME");
  728.     }
  729.       else
  730.     {
  731.       /* Handle ~ followed by user name.  */
  732.       unsigned char *user = nm + 1;
  733.       /* Find end of name.  */
  734.       unsigned char *ptr = (unsigned char *) index (user, '/');
  735.       int len = ptr ? ptr - user : strlen (user);
  736. #ifdef VMS
  737.       unsigned char *ptr1 = (unsigned char *) index (user, ':');
  738.       if (ptr1 != 0 && ptr1 - user < len)
  739.         len = ptr1 - user;
  740. #endif /* VMS */
  741.       /* Copy the user name into temp storage.  */
  742.       o = (unsigned char *) alloca (len + 1);
  743.       bcopy ((char *) user, o, len);
  744.       o[len] = 0;
  745.  
  746.       /* Look up the user name.  */
  747.       pw = (struct passwd *) getpwnam (o);
  748.       if (!pw)
  749.         error ("User \"%s\" is not known", o);
  750.       newdir = (unsigned char *) pw->pw_dir;
  751.  
  752.       /* Discard the user name from NM.  */
  753.       nm += len;
  754.     }
  755.  
  756.       /* Discard the ~ from NM.  */
  757.       nm++;
  758. #ifdef VMS
  759.       if (*nm != 0)
  760.     nm++;            /* Don't leave the slash in nm.  */
  761. #endif /* VMS */
  762.  
  763.       if (newdir == 0)
  764.     newdir = (unsigned char *) "";
  765.     }
  766.  
  767.   if (nm[0] != '/'
  768. #ifdef VMS
  769.       && !index (nm, ':')
  770. #endif /* not VMS */
  771.       && !newdir)
  772.     {
  773.       if (NULL (defalt))
  774.     defalt = current_buffer->directory;
  775.       CHECK_STRING (defalt, 1);
  776.       newdir = XSTRING (defalt)->data;
  777.     }
  778.  
  779.   if (newdir != 0)
  780.     {
  781.       /* Get rid of any slash at the end of newdir.  */
  782.       int length = strlen (newdir);
  783.       if (length > 1 && newdir[length - 1] == '/')
  784.     {
  785.       unsigned char *temp = (unsigned char *) alloca (length);
  786.       bcopy (newdir, temp, length - 1);
  787.       temp[length - 1] = 0;
  788.       newdir = temp;
  789.     }
  790.       tlen = length + 1;
  791.     }
  792.   else
  793.     tlen = 0;
  794.  
  795.   /* Now concatenate the directory and name to new space in the stack frame */
  796.  
  797.   tlen += strlen (nm) + 1;
  798.   target = (unsigned char *) alloca (tlen);
  799.   *target = 0;
  800.  
  801.   if (newdir)
  802.     {
  803. #ifndef VMS
  804.       if (nm[0] == 0 || nm[0] == '/')
  805.     strcpy (target, newdir);
  806.       else
  807. #endif
  808.       file_name_as_directory (target, newdir);
  809.     }
  810.  
  811.   strcat (target, nm);
  812. #ifdef VMS
  813.   if (index (target, '/'))
  814.     strcpy (target, sys_translate_unix (target));
  815. #endif /* VMS */
  816.  
  817.   /* Now canonicalize by removing /. and /foo/.. if they appear */
  818.  
  819.   p = target;
  820.   o = target;
  821.  
  822.   while (*p)
  823.     {
  824. #ifdef VMS
  825.       if (*p != ']' && *p != '>' && *p != '-')
  826.     {
  827.       if (*p == '\\')
  828.         p++;
  829.       *o++ = *p++;
  830.     }
  831.       else if ((p[0] == ']' || p[0] == '>') && p[0] == p[1] + 2)
  832.     /* brackets are offset from each other by 2 */
  833.     {
  834.       p += 2;
  835.       if (*p != '.' && *p != '-' && o[-1] != '.')
  836.         /* convert [foo][bar] to [bar] */
  837.         while (o[-1] != '[' && o[-1] != '<')
  838.           o--;
  839.       else if (*p == '-' && *o != '.')
  840.         *--p = '.';
  841.     }
  842.       else if (p[0] == '-' && o[-1] == '.' &&
  843.            (p[1] == '.' || p[1] == ']' || p[1] == '>'))
  844.     /* flush .foo.- ; leave - if stopped by '[' or '<' */
  845.     {
  846.       do
  847.         o--;
  848.       while (o[-1] != '.' && o[-1] != '[' && o[-1] != '<');
  849.       if (p[1] == '.')    /* foo.-.bar ==> bar*/
  850.         p += 2;
  851.       else if (o[-1] == '.') /* '.foo.-]' ==> ']' */
  852.         p++, o--;
  853.       /* else [foo.-] ==> [-] */
  854.     }
  855.       else
  856.     {
  857. #ifndef VMS4_4
  858.       if (*p == '-' &&
  859.           o[-1] != '[' && o[-1] != '<' && o[-1] != '.' &&
  860.           p[1] != ']' && p[1] != '>' && p[1] != '.')
  861.         *p = '_';
  862. #endif /* VMS4_4 */
  863.       *o++ = *p++;
  864.     }
  865. #else /* not VMS */
  866.       if (*p != '/')
  867.      {
  868.       *o++ = *p++;
  869.     }
  870.       else if (!strncmp (p, "//", 2)
  871. #ifdef APOLLO
  872.            /* // at start of filename is meaningful in Apollo system */
  873.            && o != target
  874. #endif /* APOLLO */
  875.            )
  876.     {
  877.       o = target;
  878.       p++;
  879.     }
  880.       else if (p[0] == '/' && p[1] == '.' &&
  881.            (p[2] == '/' || p[2] == 0))
  882.     p += 2;
  883.       else if (!strncmp (p, "/..", 3)
  884.            /* `/../' is the "superroot" on certain file systems.  */
  885.            && o != target
  886.            && (p[3] == '/' || p[3] == 0))
  887.     {
  888.       while (o != target && *--o != '/')
  889.         ;
  890. #ifdef APOLLO
  891.       if (o == target + 1 && o[-1] == '/' && o[0] == '/')
  892.         ++o;
  893.       else
  894. #endif APOLLO
  895.       if (o == target && *o == '/')
  896.         ++o;
  897.       p += 3;
  898.     }
  899.       else
  900.      {
  901.       *o++ = *p++;
  902.     }
  903. #endif /* not VMS */
  904.     }
  905.  
  906.   return make_string (target, o - target);
  907. #endif /* not AMIGA */
  908. }
  909.  
  910. DEFUN ("substitute-in-file-name", Fsubstitute_in_file_name,
  911.   Ssubstitute_in_file_name, 1, 1, 0,
  912.   "Substitute environment variables referred to in STRING.\n\
  913. A $ begins a request to substitute; the env variable name is the alphanumeric\n\
  914. characters and underscores after the $, or is surrounded by braces.\n\
  915. If a ~ appears following a /, everything through that / is discarded.\n\
  916. On VMS, $ substitution is not done; this function does little and only\n\
  917. duplicates what expand-file-name does.")
  918.   (string)
  919.      Lisp_Object string;
  920. {
  921.   unsigned char *nm;
  922.  
  923.   register unsigned char *s, *p, *o, *x, *endp;
  924.   unsigned char *target;
  925.   int total = 0;
  926.   int substituted = 0;
  927.   unsigned char *xnm;
  928.  
  929.   CHECK_STRING (string, 0);
  930.  
  931.   nm = XSTRING (string)->data;
  932.   endp = nm + XSTRING (string)->size;
  933.  
  934.   /* If /~ or // appears, discard everything through first slash. */
  935.  
  936.   for (p = nm; p != endp; p++)
  937.     {
  938. #ifdef AMIGA
  939.       if (p[0] == '~' && p != nm && p[-1] == '/')
  940.     {
  941.         nm = p;
  942.         substituted = 1;
  943.     }
  944.       else if (p[0] == ':')
  945.     {
  946.           char *p2 = p;
  947.       while (p2 > nm && p2[-1] != ':' && p2[-1] != '/') p2--;
  948.       if (p2 != nm)
  949.         {
  950.           nm = p2;
  951.           substituted = 1;
  952.         }
  953.     }
  954. #else /* not AMIGA */
  955.       if ((p[0] == '~' ||
  956. #ifdef APOLLO
  957.        /* // at start of file name is meaningful in Apollo system */
  958.        (p[0] == '/' && p - 1 != nm)
  959. #else /* not APOLLO */
  960.        p[0] == '/'
  961. #endif /* not APOLLO */
  962.        )
  963.       && p != nm &&
  964. #ifdef VMS
  965.       (p[-1] == ':' || p[-1] == ']' || p[-1] == '>' ||
  966. #endif /* VMS */
  967.       p[-1] == '/')
  968. #ifdef VMS
  969.       )
  970. #endif /* VMS */
  971.     {
  972.       nm = p;
  973.       substituted = 1;
  974.     }
  975. #endif /* not AMIGA */
  976.     }
  977.  
  978. #ifdef VMS
  979.   return build_string (nm);
  980. #else
  981.  
  982.   /* See if any variables are substituted into the string
  983.      and find the total length of their values in `total' */
  984.  
  985.   for (p = nm; p != endp;)
  986.     if (*p != '$')
  987.       p++;
  988.     else
  989.       {
  990.     p++;
  991.     if (p == endp)
  992.       goto badsubst;
  993.     else if (*p == '$')
  994.       {
  995.         /* "$$" means a single "$" */
  996.         p++;
  997.         total -= 1;
  998.         substituted = 1;
  999.         continue;
  1000.       }
  1001.     else if (*p == '{')
  1002.       {
  1003.         o = ++p;
  1004.         while (p != endp && *p != '}') p++;
  1005.         if (*p != '}') goto missingclose;
  1006.         s = p;
  1007.       }
  1008.     else
  1009.       {
  1010.         o = p;
  1011.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1012.         s = p;
  1013.       }
  1014.  
  1015.     /* Copy out the variable name */
  1016.     target = (unsigned char *) alloca (s - o + 1);
  1017.     strncpy (target, o, s - o);
  1018.     target[s - o] = 0;
  1019.  
  1020.     /* Get variable value */
  1021.     o = (unsigned char *) egetenv (target);
  1022. /* The presence of this code makes vax 5.0 crash, for reasons yet unknown */
  1023. #if 0
  1024. #ifdef USG
  1025.     if (!o && !strcmp (target, "USER"))
  1026.       o = egetenv ("LOGNAME");
  1027. #endif /* USG */
  1028. #endif /* 0 */
  1029.     if (!o) goto badvar;
  1030.     total += strlen (o);
  1031.     substituted = 1;
  1032.       }
  1033.  
  1034.   if (!substituted)
  1035.     return string;
  1036.  
  1037.   /* If substitution required, recopy the string and do it */
  1038.   /* Make space in stack frame for the new copy */
  1039.   xnm = (unsigned char *) alloca (XSTRING (string)->size + total + 1);
  1040.   x = xnm;
  1041.  
  1042.   /* Copy the rest of the name through, replacing $ constructs with values */
  1043.   for (p = nm; *p;)
  1044.     if (*p != '$')
  1045.       *x++ = *p++;
  1046.     else
  1047.       {
  1048.     p++;
  1049.     if (p == endp)
  1050.       goto badsubst;
  1051.     else if (*p == '$')
  1052.       {
  1053.         *x++ = *p++;
  1054.         continue;
  1055.       }
  1056.     else if (*p == '{')
  1057.       {
  1058.         o = ++p;
  1059.         while (p != endp && *p != '}') p++;
  1060.         if (*p != '}') goto missingclose;
  1061.         s = p++;
  1062.       }
  1063.     else
  1064.       {
  1065.         o = p;
  1066.         while (p != endp && (isalnum (*p) || *p == '_')) p++;
  1067.         s = p;
  1068.       }
  1069.  
  1070.     /* Copy out the variable name */
  1071.     target = (unsigned char *) alloca (s - o + 1);
  1072.     strncpy (target, o, s - o);
  1073.     target[s - o] = 0;
  1074.  
  1075.     /* Get variable value */
  1076.     o = (unsigned char *) egetenv (target);
  1077. /* The presence of this code makes vax 5.0 crash, for reasons yet unknown */
  1078. #if 0
  1079. #ifdef USG
  1080.     if (!o && !strcmp (target, "USER"))
  1081.       o = egetenv ("LOGNAME");
  1082. #endif /* USG */
  1083. #endif /* 0 */
  1084.     if (!o)
  1085.       goto badvar;
  1086.  
  1087.     strcpy (x, o);
  1088.     x += strlen (o);
  1089.       }
  1090.  
  1091.   *x = 0;
  1092.  
  1093.   /* If /~ or // appears, discard everything through first slash. */
  1094.  
  1095.   for (p = xnm; p != x; p++)
  1096.     if ((p[0] == '~' ||
  1097. #ifdef APOLLO
  1098.      /* // at start of file name is meaningful in Apollo system */
  1099.      (p[0] == '/' && p - 1 != xnm)
  1100. #else /* not APOLLO */
  1101.      p[0] == '/'
  1102. #endif /* not APOLLO */
  1103.      )
  1104.     && p != nm && p[-1] == '/')
  1105.       xnm = p;
  1106.  
  1107.   return make_string (xnm, x - xnm);
  1108.  
  1109.  badsubst:
  1110.   error ("Bad format environment-variable substitution");
  1111.  missingclose:
  1112.   error ("Missing \"}\" in environment-variable substitution");
  1113.  badvar:
  1114.   error ("Substituting nonexistent environment variable \"%s\"", target);
  1115.  
  1116.   /* NOTREACHED */
  1117. #endif /* not VMS */
  1118. }
  1119.  
  1120. Lisp_Object
  1121. expand_and_dir_to_file (filename, defdir)
  1122.      Lisp_Object filename, defdir;
  1123. {
  1124.   register Lisp_Object abspath;
  1125.  
  1126.   abspath = Fexpand_file_name (filename, defdir);
  1127. #ifdef VMS
  1128.   {
  1129.     register int c = XSTRING (abspath)->data[XSTRING (abspath)->size - 1];
  1130.     if (c == ':' || c == ']' || c == '>')
  1131.       abspath = Fdirectory_file_name (abspath);
  1132.   }
  1133. #else
  1134.   /* Remove final slash, if any (unless path is root).
  1135.      stat behaves differently depending!  */
  1136.   if (XSTRING (abspath)->size > 1
  1137.       && XSTRING (abspath)->data[XSTRING (abspath)->size - 1] == '/')
  1138.     {
  1139.       if (EQ (abspath, filename))
  1140.     abspath = Fcopy_sequence (abspath);
  1141.       XSTRING (abspath)->data[XSTRING (abspath)->size - 1] = 0;
  1142.     }
  1143. #endif
  1144.   return abspath;
  1145. }
  1146.  
  1147. barf_or_query_if_file_exists (absname, querystring, interactive)
  1148.      Lisp_Object absname;
  1149.      unsigned char *querystring;
  1150.      int interactive;
  1151. {
  1152.   register Lisp_Object tem;
  1153.   struct gcpro gcpro1;
  1154.  
  1155.   if (access (XSTRING (absname)->data, 4) >= 0)
  1156.     {
  1157.       if (! interactive)
  1158.     Fsignal (Qfile_already_exists,
  1159.          Fcons (build_string ("File already exists"),
  1160.             Fcons (absname, Qnil)));
  1161.       GCPRO1 (absname);
  1162.       tem = Fyes_or_no_p (format1 ("File %s already exists; %s anyway? ",
  1163.                    XSTRING (absname)->data, querystring));
  1164.       UNGCPRO;
  1165.       if (NULL (tem))
  1166.     Fsignal (Qfile_already_exists,
  1167.          Fcons (build_string ("File already exists"),
  1168.             Fcons (absname, Qnil)));
  1169.     }
  1170.   return;
  1171. }
  1172.  
  1173. DEFUN ("copy-file", Fcopy_file, Scopy_file, 2, 4,
  1174.   "fCopy file: \nFCopy %s to file: \np",
  1175.   "Copy FILE to NEWNAME.  Both args strings.\n\
  1176. Signals a  file-already-exists  error if NEWNAME already exists,\n\
  1177. unless a third argument OK-IF-ALREADY-EXISTS is supplied and non-nil.\n\
  1178. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1179. This is what happens in interactive use with M-x.\n\
  1180. Fourth arg non-nil means give the new file the same last-modified time\n\
  1181. that the old one has.  (This works on only some systems.)")
  1182.   (filename, newname, ok_if_already_exists, keep_date)
  1183.      Lisp_Object filename, newname, ok_if_already_exists, keep_date;
  1184. {
  1185.   int ifd, ofd, n;
  1186.   char buf[16 * 1024];
  1187.   struct stat st;
  1188.   struct gcpro gcpro1, gcpro2;
  1189.  
  1190.   GCPRO2 (filename, newname);
  1191.   CHECK_STRING (filename, 0);
  1192.   CHECK_STRING (newname, 1);
  1193.   filename = Fexpand_file_name (filename, Qnil);
  1194.   newname = Fexpand_file_name (newname, Qnil);
  1195.   if (NULL (ok_if_already_exists)
  1196.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1197.     barf_or_query_if_file_exists (newname, "copy to it",
  1198.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1199.  
  1200.   ifd = open (XSTRING (filename)->data, 0);
  1201.   if (ifd < 0)
  1202.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1203.  
  1204. #ifdef VMS
  1205.   /* Create the copy file with the same record format as the input file */
  1206.   ofd = sys_creat (XSTRING (newname)->data, 0666, ifd);
  1207. #else
  1208.   ofd = creat (XSTRING (newname)->data, 0666);
  1209. #endif /* VMS */
  1210.   if (ofd < 0)
  1211.     {
  1212.       close (ifd);
  1213.       report_file_error ("Opening output file", Fcons (newname, Qnil));
  1214.     }
  1215.  
  1216.   while ((n = read (ifd, buf, sizeof buf)) > 0)
  1217.     if (write (ofd, buf, n) != n)
  1218.       {
  1219.     close (ifd);
  1220.     close (ofd);
  1221.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1222.       }
  1223.  
  1224.   if (fstat (ifd, &st) >= 0)
  1225.     {
  1226. #ifdef HAVE_TIMEVAL
  1227.       if (!NULL (keep_date))
  1228.     {
  1229. #ifdef USE_UTIME
  1230. /* AIX has utimes() in compatibility package, but it dies.  So use good old
  1231.    utime interface instead. */
  1232.       struct {
  1233.         time_t atime;
  1234.         time_t mtime;
  1235.       } tv;
  1236.       tv.atime = st.st_atime;
  1237.       tv.mtime = st.st_mtime;
  1238.       utime (XSTRING (newname)->data, &tv);
  1239. #else /* not USE_UTIME */
  1240.       struct timeval timevals[2];
  1241.       timevals[0].tv_sec = st.st_atime;
  1242.       timevals[1].tv_sec = st.st_mtime;
  1243.       timevals[0].tv_usec = timevals[1].tv_usec = 0;
  1244.       utimes (XSTRING (newname)->data, timevals);
  1245. #endif /* not USE_UTIME */
  1246.     }
  1247. #endif /* HAVE_TIMEVALS */
  1248.  
  1249. #ifdef APOLLO
  1250.       if (!egetenv ("USE_DOMAIN_ACLS"))
  1251. #endif
  1252.       chmod (XSTRING (newname)->data, st.st_mode & 07777);
  1253.     }
  1254.  
  1255.   close (ifd);
  1256.   if (close (ofd) < 0)
  1257.     report_file_error ("I/O error", Fcons (newname, Qnil));
  1258.  
  1259.   UNGCPRO;
  1260.   return Qnil;
  1261. }
  1262.  
  1263. DEFUN ("delete-file", Fdelete_file, Sdelete_file, 1, 1, "fDelete file: ",
  1264.   "Delete specified file.  One argument, a file name string.\n\
  1265. If file has multiple names, it continues to exist with the other names.")
  1266.   (filename)
  1267.      Lisp_Object filename;
  1268. {
  1269.   CHECK_STRING (filename, 0);
  1270.   filename = Fexpand_file_name (filename, Qnil);
  1271.   if (0 > unlink (XSTRING (filename)->data))
  1272.     report_file_error ("Removing old name", Flist (1, &filename));
  1273.   return Qnil;
  1274. }
  1275.  
  1276. DEFUN ("rename-file", Frename_file, Srename_file, 2, 3,
  1277.   "fRename file: \nFRename %s to file: \np",
  1278.   "Rename FILE as NEWNAME.  Both args strings.\n\
  1279. If file has names other than FILE, it continues to have those names.\n\
  1280. Signals a  file-already-exists  error if NEWNAME already exists\n\
  1281. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1282. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1283. This is what happens in interactive use with M-x.")
  1284.   (filename, newname, ok_if_already_exists)
  1285.      Lisp_Object filename, newname, ok_if_already_exists;
  1286. {
  1287. #ifdef NO_ARG_ARRAY
  1288.   Lisp_Object args[2];
  1289. #endif
  1290.   struct gcpro gcpro1, gcpro2;
  1291.  
  1292.   GCPRO2 (filename, newname);
  1293.   CHECK_STRING (filename, 0);
  1294.   CHECK_STRING (newname, 1);
  1295.   filename = Fexpand_file_name (filename, Qnil);
  1296.   newname = Fexpand_file_name (newname, Qnil);
  1297.   if (NULL (ok_if_already_exists)
  1298.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1299.     barf_or_query_if_file_exists (newname, "rename to it",
  1300.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1301. #ifndef BSD4_1
  1302.   if (0 > rename (XSTRING (filename)->data, XSTRING (newname)->data))
  1303. #else
  1304.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data)
  1305.       || 0 > unlink (XSTRING (filename)->data))
  1306. #endif
  1307.     {
  1308.       if (errno == EXDEV)
  1309.     {
  1310.       Fcopy_file (filename, newname, ok_if_already_exists, Qt);
  1311.       Fdelete_file (filename);
  1312.     }
  1313.       else
  1314. #ifdef NO_ARG_ARRAY
  1315.     {
  1316.       args[0] = filename;
  1317.       args[1] = newname;
  1318.       report_file_error ("Renaming", Flist (2, args));
  1319.     }
  1320. #else
  1321.     report_file_error ("Renaming", Flist (2, &filename));
  1322. #endif
  1323.     }
  1324.   UNGCPRO;
  1325.   return Qnil;
  1326. }
  1327.  
  1328. DEFUN ("add-name-to-file", Fadd_name_to_file, Sadd_name_to_file, 2, 3,
  1329.   "fAdd name to file: \nFName to add to %s: \np",
  1330.   "Give FILE additional name NEWNAME.  Both args strings.\n\
  1331. Signals a  file-already-exists  error if NEWNAME already exists\n\
  1332. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1333. A number as third arg means request confirmation if NEWNAME already exists.\n\
  1334. This is what happens in interactive use with M-x.")
  1335.   (filename, newname, ok_if_already_exists)
  1336.      Lisp_Object filename, newname, ok_if_already_exists;
  1337. {
  1338. #ifdef NO_ARG_ARRAY
  1339.   Lisp_Object args[2];
  1340. #endif
  1341.   struct gcpro gcpro1, gcpro2;
  1342.  
  1343.   GCPRO2 (filename, newname);
  1344.   CHECK_STRING (filename, 0);
  1345.   CHECK_STRING (newname, 1);
  1346.   filename = Fexpand_file_name (filename, Qnil);
  1347.   newname = Fexpand_file_name (newname, Qnil);
  1348.   if (NULL (ok_if_already_exists)
  1349.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1350.     barf_or_query_if_file_exists (newname, "make it a new name",
  1351.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1352.   unlink (XSTRING (newname)->data);
  1353.   if (0 > link (XSTRING (filename)->data, XSTRING (newname)->data))
  1354.     {
  1355. #ifdef NO_ARG_ARRAY
  1356.       args[0] = filename;
  1357.       args[1] = newname;
  1358.       report_file_error ("Adding new name", Flist (2, args));
  1359. #else
  1360.       report_file_error ("Adding new name", Flist (2, &filename));
  1361. #endif
  1362.     }
  1363.  
  1364.   UNGCPRO;
  1365.   return Qnil;
  1366. }
  1367.  
  1368. #ifdef S_IFLNK
  1369. DEFUN ("make-symbolic-link", Fmake_symbolic_link, Smake_symbolic_link, 2, 3,
  1370.   "sMake symbolic link to file: \nFMake symbolic link to file %s: \np",
  1371.   "Make a symbolic link to TARGET, named LINKNAME.  Both args strings.\n\
  1372. There is no completion for LINKNAME, because it is read simply as a string;\n\
  1373. this is to enable you to make a link to a relative file name.\n\n\
  1374. Signals a  file-already-exists  error if LINKNAME already exists\n\
  1375. unless optional third argument OK-IF-ALREADY-EXISTS is non-nil.\n\
  1376. A number as third arg means request confirmation if LINKNAME already exists.\n\
  1377. This happens for interactive use with M-x.")
  1378.   (filename, newname, ok_if_already_exists)
  1379.      Lisp_Object filename, newname, ok_if_already_exists;
  1380. {
  1381. #ifdef NO_ARG_ARRAY
  1382.   Lisp_Object args[2];
  1383. #endif
  1384.   struct gcpro gcpro1, gcpro2;
  1385.  
  1386.   GCPRO2 (filename, newname);
  1387.   CHECK_STRING (filename, 0);
  1388.   CHECK_STRING (newname, 1);
  1389. #if 0 /* This made it impossible to make a link to a relative name.  */
  1390.   filename = Fexpand_file_name (filename, Qnil);
  1391. #endif
  1392.   newname = Fexpand_file_name (newname, Qnil);
  1393.   if (NULL (ok_if_already_exists)
  1394.       || XTYPE (ok_if_already_exists) == Lisp_Int)
  1395.     barf_or_query_if_file_exists (newname, "make it a link",
  1396.                   XTYPE (ok_if_already_exists) == Lisp_Int);
  1397.   if (0 > symlink (XSTRING (filename)->data, XSTRING (newname)->data))
  1398.     {
  1399.       int failure = 1;
  1400.       /* If we failed because the link name already exists,
  1401.      try deleting it.  */
  1402.       if (errno == EEXIST)
  1403.     {
  1404.       unlink (XSTRING (newname)->data);
  1405.       failure = 0 > symlink (XSTRING (filename)->data,
  1406.                  XSTRING (newname)->data);
  1407.     }
  1408.       /* If we have not started to win, report the error.  */
  1409.       if (failure)
  1410.     {
  1411. #ifdef NO_ARG_ARRAY
  1412.       args[0] = filename;
  1413.       args[1] = newname;
  1414.       report_file_error ("Making symbolic link", Flist (2, args));
  1415. #else
  1416.       report_file_error ("Making symbolic link", Flist (2, &filename));
  1417. #endif
  1418.     }
  1419.     }
  1420.   UNGCPRO;
  1421.   return Qnil;
  1422. }
  1423. #endif /* S_IFLNK */
  1424.  
  1425. #ifdef VMS
  1426.  
  1427. DEFUN ("define-logical-name", Fdefine_logical_name, Sdefine_logical_name,
  1428.        2, 2,
  1429.        "sDefine logical name: \nsDefine logical name %s as: ",
  1430.        "Define the job-wide logical name NAME to have the value STRING.\n\
  1431. If STRING is nil or a null string, the logical name NAME is deleted.")
  1432.   (varname, string)
  1433.      Lisp_Object varname;
  1434.      Lisp_Object string;
  1435. {
  1436.   CHECK_STRING (varname, 0);
  1437.   if (NULL (string))
  1438.     delete_logical_name (XSTRING (varname)->data);
  1439.   else
  1440.     {
  1441.       CHECK_STRING (string, 1);
  1442.  
  1443.       if (XSTRING (string)->size == 0)
  1444.         delete_logical_name (XSTRING (varname)->data);
  1445.       else
  1446.         define_logical_name (XSTRING (varname)->data, XSTRING (string)->data);
  1447.     }
  1448.  
  1449.   return string;
  1450. }
  1451. #endif /* VMS */
  1452.  
  1453. #ifdef HPUX_NET
  1454.  
  1455. DEFUN ("sysnetunam", Fsysnetunam, Ssysnetunam, 2, 2, 0,
  1456.        "Open a network connection to PATH using LOGIN as the login string.")
  1457.      (path, login)
  1458.      Lisp_Object path, login;
  1459. {
  1460.   int netresult;
  1461.   
  1462.   CHECK_STRING (path, 0);
  1463.   CHECK_STRING (login, 0);  
  1464.   
  1465.   netresult = netunam (XSTRING (path)->data, XSTRING (login)->data);
  1466.  
  1467.   if (netresult == -1)
  1468.     return Qnil;
  1469.   else
  1470.     return Qt;
  1471. }
  1472. #endif /* HPUX_NET */
  1473.  
  1474. DEFUN ("file-name-absolute-p", Ffile_name_absolute_p, Sfile_name_absolute_p,
  1475.        1, 1, 0,
  1476.        "Return t if file FILENAME specifies an absolute path name.")
  1477.      (filename)
  1478.      Lisp_Object filename;
  1479. {
  1480.   unsigned char *ptr;
  1481.  
  1482.   CHECK_STRING (filename, 0);
  1483.   ptr = XSTRING (filename)->data;
  1484. #ifdef    AMIGA
  1485.   /* An absolute filename has a non-leading ':' in it */
  1486.   if (*ptr != ':')
  1487.       while (*ptr)
  1488.       if (*ptr++ == ':') return Qt;
  1489.   return Qnil;
  1490. #else    /* not AMIGA */
  1491.   if (*ptr == '/' || *ptr == '~'
  1492. #ifdef VMS
  1493. /* ??? This criterion is probably wrong for '<'.  */
  1494.       || index (ptr, ':') || index (ptr, '<')
  1495.       || (*ptr == '[' && (ptr[1] != '-' || (ptr[2] != '.' && ptr[2] != ']'))
  1496.       && ptr[1] != '.')
  1497. #endif /* VMS */
  1498.       )
  1499.     return Qt;
  1500.   else
  1501.     return Qnil;
  1502. #endif /* not AMIGA */
  1503. }
  1504.  
  1505. DEFUN ("file-exists-p", Ffile_exists_p, Sfile_exists_p, 1, 1, 0,
  1506.   "Return t if file FILENAME exists.  (This does not mean you can read it.)\n\
  1507. See also file-readable-p and file-attributes.")
  1508.   (filename)
  1509.      Lisp_Object filename;
  1510. {
  1511.   Lisp_Object abspath;
  1512.   struct stat sb;
  1513.  
  1514.   CHECK_STRING (filename, 0);
  1515.   abspath = Fexpand_file_name (filename, Qnil);
  1516. #ifdef S_IFLNK
  1517.   return (lstat (XSTRING (abspath)->data, &sb) >= 0) ? Qt : Qnil;
  1518. #else
  1519.   return (stat (XSTRING (abspath)->data, &sb) >= 0) ? Qt : Qnil;
  1520. #endif
  1521. }
  1522.  
  1523. DEFUN ("file-readable-p", Ffile_readable_p, Sfile_readable_p, 1, 1, 0,
  1524.   "Return t if file FILENAME exists and you can read it.\n\
  1525. See also file-exists-p and file-attributes.")
  1526.   (filename)
  1527.      Lisp_Object filename;
  1528. {
  1529.   Lisp_Object abspath;
  1530.  
  1531.   CHECK_STRING (filename, 0);
  1532.   abspath = Fexpand_file_name (filename, Qnil);
  1533.   return (access (XSTRING (abspath)->data, 4) >= 0) ? Qt : Qnil;
  1534. }
  1535.  
  1536. DEFUN ("file-symlink-p", Ffile_symlink_p, Sfile_symlink_p, 1, 1, 0,
  1537.   "If file FILENAME is the name of a symbolic link\n\
  1538. returns the name of the file to which it is linked.\n\
  1539. Otherwise returns NIL.")
  1540.   (filename)
  1541.      Lisp_Object filename;
  1542. {
  1543. #ifdef S_IFLNK
  1544.   char *buf;
  1545.   int bufsize;
  1546.   int valsize;
  1547.   Lisp_Object val;
  1548.  
  1549.   CHECK_STRING (filename, 0);
  1550.   filename = Fexpand_file_name (filename, Qnil);
  1551.  
  1552.   bufsize = 100;
  1553.   while (1)
  1554.     {
  1555.       buf = (char *) xmalloc (bufsize);
  1556.       bzero (buf, bufsize);
  1557.       valsize = readlink (XSTRING (filename)->data, buf, bufsize);
  1558.       if (valsize < bufsize) break;
  1559.       /* Buffer was not long enough */
  1560.       free (buf);
  1561.       bufsize *= 2;
  1562.     }
  1563.   if (valsize == -1)
  1564.     {
  1565.       free (buf);
  1566.       return Qnil;
  1567.     }
  1568.   val = make_string (buf, valsize);
  1569.   free (buf);
  1570.   return val;
  1571. #else /* not S_IFLNK */
  1572.   return Qnil;
  1573. #endif /* not S_IFLNK */
  1574. }
  1575.  
  1576. /* Having this before file-symlink-p mysteriously caused it to be forgotten
  1577.    on the RT/PC.  */
  1578. DEFUN ("file-writable-p", Ffile_writable_p, Sfile_writable_p, 1, 1, 0,
  1579.   "Return t if file FILENAME can be written or created by you.")
  1580.   (filename)
  1581.      Lisp_Object filename;
  1582. {
  1583.   Lisp_Object abspath, dir;
  1584.  
  1585.   CHECK_STRING (filename, 0);
  1586.   abspath = Fexpand_file_name (filename, Qnil);
  1587.   if (access (XSTRING (abspath)->data, 0) >= 0)
  1588.     return (access (XSTRING (abspath)->data, 2) >= 0) ? Qt : Qnil;
  1589.   dir = Ffile_name_directory (abspath);
  1590. #ifdef VMS
  1591.   if (!NULL (dir))
  1592.     dir = Fdirectory_file_name (dir);
  1593. #endif /* VMS */
  1594.   return (access (!NULL (dir) ? (char *) XSTRING (dir)->data : "", 2) >= 0
  1595.       ? Qt : Qnil);
  1596. }
  1597.  
  1598. DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0,
  1599.   "Return t if file FILENAME is the name of a directory as a file.\n\
  1600. A directory name spec may be given instead; then the value is t\n\
  1601. if the directory so specified exists and really is a directory.")
  1602.   (filename)
  1603.      Lisp_Object filename;
  1604. {
  1605.   register Lisp_Object abspath;
  1606.   struct stat st;
  1607.  
  1608.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  1609.  
  1610.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1611.     return Qnil;
  1612.   return (st.st_mode & S_IFMT) == S_IFDIR ? Qt : Qnil;
  1613. }
  1614.  
  1615. DEFUN ("file-modes", Ffile_modes, Sfile_modes, 1, 1, 0,
  1616.   "Return mode bits of FILE, as an integer.")
  1617.   (filename)
  1618.      Lisp_Object filename;
  1619. {
  1620.   Lisp_Object abspath;
  1621.   struct stat st;
  1622.  
  1623.   abspath = expand_and_dir_to_file (filename, current_buffer->directory);
  1624.  
  1625.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1626.     return Qnil;
  1627.   return make_number (st.st_mode & 07777);
  1628. }
  1629.  
  1630. DEFUN ("set-file-modes", Fset_file_modes, Sset_file_modes, 2, 2, 0,
  1631.   "Set mode bits of FILE to MODE (an integer).\n\
  1632. Only the 12 low bits of MODE are used.")
  1633.   (filename, mode)
  1634.      Lisp_Object filename, mode;
  1635. {
  1636.   Lisp_Object abspath;
  1637.  
  1638.   abspath = Fexpand_file_name (filename, current_buffer->directory);
  1639.   CHECK_NUMBER (mode, 1);
  1640.  
  1641. #ifndef APOLLO
  1642.   if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  1643.     report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  1644. #else /* APOLLO */
  1645.   if (!egetenv ("USE_DOMAIN_ACLS"))
  1646.     {
  1647.       struct stat st;
  1648.       struct timeval tvp[2];
  1649.  
  1650.       /* chmod on apollo also change the file's modtime; need to save the
  1651.      modtime and then restore it. */
  1652.       if (stat (XSTRING (abspath)->data, &st) < 0)
  1653.     {
  1654.       report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  1655.       return (Qnil);
  1656.     }
  1657.  
  1658.       if (chmod (XSTRING (abspath)->data, XINT (mode)) < 0)
  1659.      report_file_error ("Doing chmod", Fcons (abspath, Qnil));
  1660.  
  1661.       /* reset the old accessed and modified times.  */
  1662.       tvp[0].tv_sec = st.st_atime + 1; /* +1 due to an Apollo roundoff bug */
  1663.       tvp[0].tv_usec = 0;
  1664.       tvp[1].tv_sec = st.st_mtime + 1; /* +1 due to an Apollo roundoff bug */
  1665.       tvp[1].tv_usec = 0;
  1666.  
  1667.       if (utimes (XSTRING (abspath)->data, tvp) < 0)
  1668.      report_file_error ("Doing utimes", Fcons (abspath, Qnil));
  1669.     }
  1670. #endif /* APOLLO */
  1671.  
  1672.   return Qnil;
  1673. }
  1674.  
  1675. DEFUN ("file-newer-than-file-p", Ffile_newer_than_file_p, Sfile_newer_than_file_p, 2, 2, 0,
  1676.   "Return t if file FILE1 is newer than file FILE2.\n\
  1677. If FILE1 does not exist, the answer is nil;\n\
  1678. otherwise, if FILE2 does not exist, the answer is t.")
  1679.   (file1, file2)
  1680.      Lisp_Object file1, file2;
  1681. {
  1682.   Lisp_Object abspath;
  1683.   struct stat st;
  1684.   int mtime1;
  1685.  
  1686.   CHECK_STRING (file1, 0);
  1687.   CHECK_STRING (file2, 0);
  1688.  
  1689.   abspath = expand_and_dir_to_file (file1, current_buffer->directory);
  1690.  
  1691.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1692.     return Qnil;
  1693.  
  1694.   mtime1 = st.st_mtime;
  1695.  
  1696.   abspath = expand_and_dir_to_file (file2, current_buffer->directory);
  1697.  
  1698.   if (stat (XSTRING (abspath)->data, &st) < 0)
  1699.     return Qt;
  1700.  
  1701.   return (mtime1 > st.st_mtime) ? Qt : Qnil;
  1702. }
  1703.  
  1704. close_file_unwind (fd)
  1705.      Lisp_Object fd;
  1706. {
  1707.   close (XFASTINT (fd));
  1708. }
  1709.  
  1710. DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents,
  1711.   1, 2, 0,
  1712.   "Insert contents of file FILENAME after point.\n\
  1713. Returns list of absolute pathname and length of data inserted.\n\
  1714. If second argument VISIT is non-nil, the buffer's visited filename\n\
  1715. and last save file modtime are set, and it is marked unmodified.\n\
  1716. If visiting and the file does not exist, visiting is completed\n\
  1717. before the error is signaled.")
  1718.   (filename, visit)
  1719.      Lisp_Object filename, visit;
  1720. {
  1721.   struct stat st;
  1722.   register int fd;
  1723.   register int inserted = 0;
  1724.   register int i = 0;
  1725.   int count = specpdl_ptr - specpdl;
  1726.   struct gcpro gcpro1;
  1727.  
  1728.   GCPRO1 (filename);
  1729.   if (!NULL (current_buffer->read_only))
  1730.     Fbarf_if_buffer_read_only();
  1731.  
  1732.   CHECK_STRING (filename, 0);
  1733.   filename = Fexpand_file_name (filename, Qnil);
  1734.  
  1735.   fd = -1;
  1736.  
  1737. #ifndef APOLLO
  1738.   if (stat (XSTRING (filename)->data, &st) < 0
  1739. #if defined (AIX) && defined (S_ISMPX)
  1740.       /* Don't let emacs open /dev/pts, as it causes kernel confusion.  */
  1741.       || S_ISMPX (st.st_mode)
  1742. #endif
  1743.       || (fd = open (XSTRING (filename)->data, 0)) < 0)
  1744. #else
  1745.   if ((fd = open (XSTRING (filename)->data, 0)) < 0
  1746.       || fstat (fd, &st) < 0)
  1747. #endif /* not APOLLO */
  1748.     {
  1749.       if (fd >= 0) close (fd);
  1750.       if (NULL (visit))
  1751.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1752.       st.st_mtime = -1;
  1753.       goto notfound;
  1754.     }
  1755.  
  1756.   record_unwind_protect (close_file_unwind, make_number (fd));
  1757.  
  1758. #ifdef VMS
  1759.   /* VAXCRTL adds implied carriage control to certain record types.  */
  1760.   if (st.st_fab_rfm == FAB$C_FIX
  1761.       && (st.st_fab_rat & (FAB$M_FTN | FAB$M_CR) != 0))
  1762.     st.st_size += st.st_size / st.st_fab_mrs;
  1763. #endif
  1764.  
  1765.   /* Supposedly happens on VMS.  */
  1766.   if (st.st_size < 0)
  1767.     error ("File size is negative");
  1768.   {
  1769.     register Lisp_Object temp;
  1770.  
  1771.     /* Make sure point-max won't overflow after this insertion.  */
  1772.     XSET (temp, Lisp_Int, st.st_size + Z);
  1773.     if (st.st_size + Z != XINT (temp))
  1774.       error ("maximum buffer size exceeded");
  1775.   }
  1776.  
  1777.   if (NULL (visit))
  1778.     prepare_to_modify_buffer ();
  1779.  
  1780.   move_gap (point);
  1781.   if (GAP_SIZE < st.st_size)
  1782.     make_gap (st.st_size - GAP_SIZE);
  1783.     
  1784.   while (1)
  1785.     {
  1786.       int try = min (st.st_size - inserted, 64 << 10);
  1787.       int this = read (fd, &FETCH_CHAR (point + inserted - 1) + 1, try);
  1788.  
  1789.       if (this <= 0)
  1790.     {
  1791.       i = this;
  1792.       break;
  1793.     }
  1794.  
  1795.       GPT += this;
  1796.       GAP_SIZE -= this;
  1797.       ZV += this;
  1798.       Z += this;
  1799.       inserted += this;
  1800.     }
  1801.  
  1802.   if (inserted > 0)
  1803.     MODIFF++;
  1804.   record_insert (point, inserted);
  1805.  
  1806.   close (fd);
  1807.  
  1808.   /* Discard the unwind protect */
  1809.   specpdl_ptr = specpdl + count;
  1810.  
  1811.   if (i < 0)
  1812.     error ("IO error reading %s: %s",
  1813.        XSTRING (filename)->data, err_str (errno));
  1814.  
  1815.  notfound:
  1816.  
  1817.   if (!NULL (visit))
  1818.     {
  1819.       current_buffer->undo_list = Qnil;
  1820. #ifdef APOLLO
  1821.       stat (XSTRING (filename)->data, &st);
  1822. #endif
  1823.       current_buffer->modtime = st.st_mtime;
  1824.       current_buffer->save_modified = MODIFF;
  1825.       current_buffer->auto_save_modified = MODIFF;
  1826.       XFASTINT (current_buffer->save_length) = Z - BEG;
  1827. #ifdef CLASH_DETECTION
  1828.       if (!NULL (current_buffer->filename))
  1829.     unlock_file (current_buffer->filename);
  1830.       unlock_file (filename);
  1831. #endif /* CLASH_DETECTION */
  1832.       current_buffer->filename = filename;
  1833.       /* If visiting nonexistent file, return nil.  */
  1834.       if (st.st_mtime == -1)
  1835.     report_file_error ("Opening input file", Fcons (filename, Qnil));
  1836.     }
  1837.  
  1838.   UNGCPRO;
  1839.   return Fcons (filename, Fcons (make_number (inserted), Qnil));
  1840. }
  1841.  
  1842. DEFUN ("write-region", Fwrite_region, Swrite_region, 3, 5,
  1843.   "r\nFWrite region to file: ",
  1844.   "Write current region into specified file.\n\
  1845. When called from a program, takes three arguments:\n\
  1846. START, END and FILENAME.  START and END are buffer positions.\n\
  1847. Optional fourth argument APPEND if non-nil means\n\
  1848.   append to existing file contents (if any).\n\
  1849. Optional fifth argument VISIT if t means\n\
  1850.   set last-save-file-modtime of buffer to this file's modtime\n\
  1851.   and mark buffer not modified.\n\
  1852. If VISIT is neither t nor nil, it means do not print\n\
  1853.   the \"Wrote file\" message.")
  1854.   (start, end, filename, append, visit)
  1855.      Lisp_Object start, end, filename, append, visit;
  1856. {
  1857.   register int desc;
  1858.   int failure;
  1859.   int save_errno;
  1860.   unsigned char *fn;
  1861.   struct stat st;
  1862.   int tem;
  1863.   int count = specpdl_ptr - specpdl;
  1864. #ifdef VMS
  1865.   unsigned char *fname = 0;    /* If non-0, original filename (must rename) */
  1866. #endif /* VMS */
  1867.  
  1868.   /* Special kludge to simplify auto-saving */
  1869.   if (NULL (start))
  1870.     {
  1871.       XFASTINT (start) = BEG;
  1872.       XFASTINT (end) = Z;
  1873.     }
  1874.   else
  1875.     validate_region (&start, &end);
  1876.  
  1877.   filename = Fexpand_file_name (filename, Qnil);
  1878.   fn = XSTRING (filename)->data;
  1879.  
  1880. #ifdef CLASH_DETECTION
  1881.   if (!auto_saving)
  1882.     lock_file (filename);
  1883. #endif /* CLASH_DETECTION */
  1884.  
  1885.   desc = -1;
  1886.   if (!NULL (append))
  1887.     desc = open (fn, O_WRONLY);
  1888.  
  1889.   if (desc < 0)
  1890. #ifdef VMS
  1891.     if (auto_saving)    /* Overwrite any previous version of autosave file */
  1892.       {
  1893.     vms_truncate (fn);    /* if fn exists, truncate to zero length */
  1894.     desc = open (fn, O_RDWR);
  1895.     if (desc < 0)
  1896.       desc = creat_copy_attrs (XTYPE (current_buffer->filename) == Lisp_String
  1897.                    ? XSTRING (current_buffer->filename)->data : 0,
  1898.                    fn);
  1899.       }
  1900.     else        /* Write to temporary name and rename if no errors */
  1901.       {
  1902.     Lisp_Object temp_name;
  1903.     temp_name = Ffile_name_directory (filename);
  1904.  
  1905.     if (!NULL (temp_name))
  1906.       {
  1907.         temp_name = Fmake_temp_name (concat2 (temp_name,
  1908.                           build_string ("$$SAVE$$")));
  1909.         fname = XSTRING (filename)->data;
  1910.         fn = XSTRING (temp_name)->data;
  1911.         desc = creat_copy_attrs (fname, fn);
  1912.         if (desc < 0)
  1913.           {
  1914.         /* If we can't open the temporary file, try creating a new
  1915.            version of the original file.  VMS "creat" creates a
  1916.            new version rather than truncating an existing file. */
  1917.         fn = fname;
  1918.         fname = 0;
  1919.         desc = creat (fn, 0666);
  1920. #if 0
  1921.         if (desc < 0)
  1922.           {
  1923.             /* We can't make a new version;
  1924.                try to truncate and rewrite existing version if any.  */
  1925.             vms_truncate (fn);
  1926.             desc = open (fn, O_RDWR);
  1927.           }
  1928. #endif
  1929.           }
  1930.       }
  1931.     else
  1932.       desc = creat (fn, 0666);
  1933.       }
  1934. #else /* not VMS */
  1935.   desc = creat (fn, 0666);
  1936. #endif /* not VMS */
  1937.  
  1938.   if (desc < 0)
  1939.     {
  1940. #ifdef CLASH_DETECTION
  1941.       save_errno = errno;
  1942.       if (!auto_saving) unlock_file (filename);
  1943.       errno = save_errno;
  1944. #endif /* CLASH_DETECTION */
  1945.       report_file_error ("Opening output file", Fcons (filename, Qnil));
  1946.     }
  1947.  
  1948.   record_unwind_protect (close_file_unwind, make_number (desc));
  1949.  
  1950.   if (!NULL (append))
  1951.     if (lseek (desc, 0, 2) < 0)
  1952.       {
  1953. #ifdef CLASH_DETECTION
  1954.     if (!auto_saving) unlock_file (filename);
  1955. #endif /* CLASH_DETECTION */
  1956.     report_file_error ("Lseek error", Fcons (filename, Qnil));
  1957.       }
  1958.  
  1959. #ifdef VMS
  1960. /*
  1961.  * Kludge Warning: The VMS C RTL likes to insert carriage returns
  1962.  * if we do writes that don't end with a carriage return. Furthermore
  1963.  * it cannot handle writes of more then 16K. The modified
  1964.  * version of "sys_write" in SYSDEP.C (see comment there) copes with
  1965.  * this EXCEPT for the last record (iff it doesn't end with a carriage
  1966.  * return). This implies that if your buffer doesn't end with a carriage
  1967.  * return, you get one free... tough. However it also means that if
  1968.  * we make two calls to sys_write (a la the following code) you can
  1969.  * get one at the gap as well. The easiest way to fix this (honest)
  1970.  * is to move the gap to the next newline (or the end of the buffer).
  1971.  * Thus this change.
  1972.  *
  1973.  * Yech!
  1974.  */
  1975.   if (GPT > BEG && GPT_ADDR[-1] != '\n')
  1976.     move_gap (find_next_newline (GPT, 1));
  1977. #endif
  1978.  
  1979.   failure = 0;
  1980.   if (XINT (start) != XINT (end))
  1981.     {
  1982.       if (XINT (start) < GPT)
  1983.     {
  1984.       register int end1 = XINT (end);
  1985.       tem = XINT (start);
  1986.       failure = 0 > e_write (desc, &FETCH_CHAR (tem),
  1987.                  min (GPT, end1) - tem);
  1988.       save_errno = errno;
  1989.     }
  1990.  
  1991.       if (XINT (end) > GPT && !failure)
  1992.     {
  1993.       tem = XINT (start);
  1994.       tem = max (tem, GPT);
  1995.       failure = 0 > e_write (desc, &FETCH_CHAR (tem), XINT (end) - tem);
  1996.       save_errno = errno;
  1997.     }
  1998.     }
  1999.  
  2000. #ifndef USG
  2001. #ifndef VMS
  2002. #ifndef BSD4_1
  2003.   /* Note fsync appears to change the modtime on BSD4.2 (both vax and sun).
  2004.      Disk full in NFS may be reported here.  */
  2005.   if (fsync (desc) < 0)
  2006.     failure = 1, save_errno = errno;
  2007. #endif
  2008. #endif
  2009. #endif
  2010.  
  2011. #if 0
  2012.   /* Spurious "file has changed on disk" warnings have been 
  2013.      observed on Sun 3 as well.  Maybe close changes the modtime
  2014.      with nfs as well.  */
  2015.  
  2016.   /* On VMS and APOLLO, must do the stat after the close
  2017.      since closing changes the modtime.  */
  2018. #ifndef VMS
  2019. #ifndef APOLLO
  2020.   /* Recall that #if defined does not work on VMS.  */
  2021. #define FOO
  2022.   fstat (desc, &st);
  2023. #endif
  2024. #endif
  2025. #endif /* 0 */
  2026.  
  2027.   /* NFS can report a write failure now.  */
  2028.   if (close (desc) < 0)
  2029.     failure = 1, save_errno = errno;
  2030.  
  2031. #ifdef VMS
  2032.   /* If we wrote to a temporary name and had no errors, rename to real name. */
  2033.   if (fname)
  2034.     {
  2035.       if (!failure)
  2036.     failure = (rename_sans_version (fn, fname) != 0), save_errno = errno;
  2037.       fn = vms_file_written;    /* this is filled by rename_sans_version */
  2038.     }
  2039. #endif /* VMS */
  2040.  
  2041. #ifndef FOO
  2042.   stat (fn, &st);
  2043. #endif
  2044.   /* Discard the unwind protect */
  2045.   specpdl_ptr = specpdl + count;
  2046.  
  2047. #ifdef CLASH_DETECTION
  2048.   if (!auto_saving)
  2049.     unlock_file (filename);
  2050. #endif /* CLASH_DETECTION */
  2051.  
  2052.   /* Do this before reporting IO error
  2053.      to avoid a "file has changed on disk" warning on
  2054.      next attempt to save.  */
  2055.   if (EQ (visit, Qt))
  2056.     current_buffer->modtime = st.st_mtime;
  2057.  
  2058.   if (failure)
  2059.     error ("IO error writing %s: %s", fn, err_str (save_errno));
  2060.  
  2061.   if (EQ (visit, Qt))
  2062.     {
  2063.       current_buffer->save_modified = MODIFF;
  2064.       XFASTINT (current_buffer->save_length) = Z - BEG;
  2065.       current_buffer->filename = filename;
  2066.     }
  2067.   else if (!NULL (visit))
  2068.     return Qnil;
  2069.  
  2070.   if (!auto_saving)
  2071.     message ("Wrote %s", fn);
  2072.  
  2073.   return Qnil;
  2074. }
  2075.  
  2076. int
  2077. e_write (desc, addr, len)
  2078.      int desc;
  2079.      register char *addr;
  2080.      register int len;
  2081. {
  2082.   char buf[16 * 1024];
  2083.   register char *p, *end;
  2084.  
  2085.   if (!EQ (current_buffer->selective_display, Qt))
  2086.     return write (desc, addr, len) - len;
  2087.   else
  2088.     {
  2089.       p = buf;
  2090.       end = p + sizeof buf;
  2091.       while (len--)
  2092.     {
  2093.       if (p == end)
  2094.         {
  2095.           if (write (desc, buf, sizeof buf) != sizeof buf)
  2096.         return -1;
  2097.           p = buf;
  2098.         }
  2099.       *p = *addr++;
  2100.       if (*p++ == '\015')
  2101.         p[-1] = '\n';
  2102.     }
  2103.       if (p != buf)
  2104.     if (write (desc, buf, p - buf) != p - buf)
  2105.       return -1;
  2106.     }
  2107.   return 0;
  2108. }
  2109.  
  2110. DEFUN ("verify-visited-file-modtime", Fverify_visited_file_modtime,
  2111.   Sverify_visited_file_modtime, 1, 1, 0,
  2112.   "Return t if last mod time of BUF's visited file matches what BUF records.\n\
  2113. This means that the file has not been changed since it was visited or saved.")
  2114.   (buf)
  2115.      Lisp_Object buf;
  2116. {
  2117.   struct buffer *b;
  2118.   struct stat st;
  2119.  
  2120.   CHECK_BUFFER (buf, 0);
  2121.   b = XBUFFER (buf);
  2122.  
  2123.   if (XTYPE (b->filename) != Lisp_String) return Qt;
  2124.   if (b->modtime == 0) return Qt;
  2125.  
  2126.   if (stat (XSTRING (b->filename)->data, &st) < 0)
  2127.     {
  2128.       /* If the file doesn't exist now and didn't exist before,
  2129.      we say that it isn't modified, provided the error is a tame one.  */
  2130.       if (errno == ENOENT || errno == EACCES || errno == ENOTDIR)
  2131.     st.st_mtime = -1;
  2132.       else
  2133.     st.st_mtime = 0;
  2134.     }
  2135.   if (st.st_mtime == b->modtime
  2136.       /* If both are positive, accept them if they are off by one second.  */
  2137.       || (st.st_mtime > 0 && b->modtime > 0
  2138.       && (st.st_mtime == b->modtime + 1
  2139.           || st.st_mtime == b->modtime - 1)))
  2140.     return Qt;
  2141.   return Qnil;
  2142. }
  2143.  
  2144. DEFUN ("clear-visited-file-modtime", Fclear_visited_file_modtime,
  2145.   Sclear_visited_file_modtime, 0, 0, 0,
  2146.   "Clear out records of last mod time of visited file.\n\
  2147. Next attempt to save will certainly not complain of a discrepancy.")
  2148.   ()
  2149. {
  2150.   current_buffer->modtime = 0;
  2151.   return Qnil;
  2152. }
  2153.  
  2154. Lisp_Object
  2155. auto_save_error ()
  2156. {
  2157.   unsigned char *name = XSTRING (current_buffer->name)->data;
  2158.  
  2159.   bell ();
  2160.   message ("Autosaving...error for %s", name);
  2161.   Fsleep_for (make_number (1));
  2162.   message ("Autosaving...error!for %s", name);
  2163.   Fsleep_for (make_number (1));
  2164.   message ("Autosaving...error for %s", name);
  2165.   Fsleep_for (make_number (1));
  2166.   return Qnil;
  2167. }
  2168.  
  2169. Lisp_Object
  2170. auto_save_1 ()
  2171. {
  2172.   return
  2173.     Fwrite_region (Qnil, Qnil,
  2174.            current_buffer->auto_save_file_name,
  2175.            Qnil, Qlambda);
  2176. }
  2177.  
  2178. DEFUN ("do-auto-save", Fdo_auto_save, Sdo_auto_save, 0, 1, "",
  2179.   "Auto-save all buffers that need it.\n\
  2180. This is all buffers that have auto-saving enabled\n\
  2181. and are changed since last auto-saved.\n\
  2182. Auto-saving writes the buffer into a file\n\
  2183. so that your editing is not lost if the system crashes.\n\
  2184. This file is not the file you visited; that changes only when you save.\n\n\
  2185. Non-nil argument means do not print any message if successful.")
  2186.   (nomsg)
  2187.      Lisp_Object nomsg;
  2188. {
  2189.   struct buffer *old = current_buffer, *b;
  2190.   Lisp_Object tail, buf;
  2191.   int auto_saved = 0;
  2192.   int tried = 0;
  2193.   char *omessage = echo_area_contents;
  2194.   /* No GCPRO needed, because (when it matters) all Lisp_Object variables
  2195.      point to non-strings reached from Vbuffer_alist.  */
  2196.  
  2197.   auto_saving = 1;
  2198.   if (minibuf_level)
  2199.     nomsg = Qt;
  2200.  
  2201.   for (tail = Vbuffer_alist; XGCTYPE (tail) == Lisp_Cons;
  2202.        tail = XCONS (tail)->cdr)
  2203.     {
  2204.       buf = XCONS (XCONS (tail)->car)->cdr;
  2205.       b = XBUFFER (buf);
  2206.       /* Check for auto save enabled
  2207.      and file changed since last auto save
  2208.      and file changed since last real save.  */
  2209.       if (XTYPE (b->auto_save_file_name) == Lisp_String
  2210.       && b->save_modified < BUF_MODIFF (b)
  2211.       && b->auto_save_modified < BUF_MODIFF (b))
  2212.     {
  2213.       /* If we at least consider a buffer for auto-saving,
  2214.          don't try again for a suitable time.  */
  2215.       tried++;
  2216.       if ((XFASTINT (b->save_length) * 10
  2217.            > (BUF_Z (b) - BUF_BEG (b)) * 13)
  2218.           /* A short file is likely to change a large fraction;
  2219.          spare the user annoying messages.  */
  2220.           && XFASTINT (b->save_length) > 5000
  2221.           /* These messages are frequent and annoying for `*mail*'.  */
  2222.           && !EQ (b->filename, Qnil))
  2223.         {
  2224.           /* It has shrunk too much; don't checkpoint. */
  2225.           message ("Buffer %s has shrunk a lot; not autosaving it",
  2226.                XSTRING (b->name)->data);
  2227.           Fsleep_for (make_number (1));
  2228.           continue;
  2229.         }
  2230.       set_buffer_internal (b);
  2231.       if (!auto_saved && NULL (nomsg))
  2232.         message1 ("Auto-saving...");
  2233.       internal_condition_case (auto_save_1, Qt, auto_save_error);
  2234.       auto_saved++;
  2235.       b->auto_save_modified = BUF_MODIFF (b);
  2236.       XFASTINT (current_buffer->save_length) = Z - BEG;
  2237.       set_buffer_internal (old);
  2238.     }
  2239.     }
  2240.  
  2241.   if (tried)
  2242.     record_auto_save ();
  2243.  
  2244.   if (auto_saved && NULL (nomsg))
  2245.     message1 (omessage ? omessage : "Auto-saving...done");
  2246.  
  2247.   auto_saving = 0;
  2248.   return Qnil;
  2249. }
  2250.  
  2251. DEFUN ("set-buffer-auto-saved", Fset_buffer_auto_saved,
  2252.   Sset_buffer_auto_saved, 0, 0, 0,
  2253.   "Mark current buffer as auto-saved with its current text.\n\
  2254. No auto-save file will be written until the buffer changes again.")
  2255.   ()
  2256. {
  2257.   current_buffer->auto_save_modified = MODIFF;
  2258.   XFASTINT (current_buffer->save_length) = Z - BEG;
  2259.   return Qnil;
  2260. }
  2261.  
  2262. DEFUN ("recent-auto-save-p", Frecent_auto_save_p, Srecent_auto_save_p,
  2263.   0, 0, 0,
  2264.   "Return t if buffer has been auto-saved since last read in or saved.")
  2265.   ()
  2266. {
  2267.   return (current_buffer->save_modified < current_buffer->auto_save_modified) ? Qt : Qnil;
  2268. }
  2269.  
  2270. /* Reading and completing file names */
  2271. extern Lisp_Object Ffile_name_completion (), Ffile_name_all_completions ();
  2272.  
  2273. DEFUN ("read-file-name-internal", Fread_file_name_internal, Sread_file_name_internal,
  2274.   3, 3, 0,
  2275.   "Internal subroutine for read-file-name.  Do not call this.")
  2276.   (string, dir, action)
  2277.      Lisp_Object string, dir, action;
  2278.   /* action is nil for complete, t for return list of completions,
  2279.      lambda for verify final value */
  2280. {
  2281.   Lisp_Object name, specdir, realdir, val;
  2282.   if (XSTRING (string)->size == 0)
  2283.     {
  2284.       name = string;
  2285.       realdir = dir;
  2286.       if (EQ (action, Qlambda))
  2287.     return Qnil;
  2288.     }
  2289.   else
  2290.     {
  2291.       string = Fsubstitute_in_file_name (string);
  2292.       name = Ffile_name_nondirectory (string);
  2293.       realdir = Ffile_name_directory (string);
  2294.       if (NULL (realdir))
  2295.     realdir = dir;
  2296.       else
  2297.     realdir = Fexpand_file_name (realdir, dir);
  2298.     }
  2299.  
  2300.   if (NULL (action))
  2301.     {
  2302.       specdir = Ffile_name_directory (string);
  2303.       val = Ffile_name_completion (name, realdir);
  2304.       if (XTYPE (val) != Lisp_String)
  2305.     return (val);
  2306.  
  2307.       if (!NULL (specdir))
  2308.     val = concat2 (specdir, val);
  2309. #ifndef VMS
  2310.       {
  2311.     register unsigned char *old, *new;
  2312.     register int n;
  2313.     int osize, count;
  2314.  
  2315.     osize = XSTRING (val)->size;
  2316.     /* Quote "$" as "$$" to get it past substitute-in-file-name */
  2317.     for (n = osize, count = 0, old = XSTRING (val)->data; n > 0; n--)
  2318.       if (*old++ == '$') count++;
  2319.     if (count > 0)
  2320.       {
  2321.         old = XSTRING (val)->data;
  2322.         val = Fmake_string (make_number (osize + count), make_number (0));
  2323.         new = XSTRING (val)->data;
  2324.         for (n = osize; n > 0; n--)
  2325.           if (*old != '$')
  2326.         *new++ = *old++;
  2327.           else
  2328.         {
  2329.           *new++ = '$';
  2330.           *new++ = '$';
  2331.           old++;
  2332.         }
  2333.       }
  2334.       }
  2335. #endif /* Not VMS */
  2336.       return (val);
  2337.     }
  2338.  
  2339.   if (EQ (action, Qt))
  2340.     return Ffile_name_all_completions (name, realdir);
  2341.   /* Only other case actually used is ACTION = lambda */
  2342. #ifdef VMS
  2343.   /* Supposedly this helps commands such as `cd' that read directory names,
  2344.      but can someone explain how it helps them? -- RMS */
  2345.   if (XSTRING (name)->size == 0)
  2346.     return Qt;
  2347. #endif /* VMS */
  2348.   return Ffile_exists_p (string);
  2349. }
  2350.  
  2351. DEFUN ("read-file-name", Fread_file_name, Sread_file_name, 1, 4, 0,
  2352.   "Read file name, prompting with PROMPT and completing in directory DIR.\n\
  2353. Value is not expanded!  You must call expand-file-name yourself.\n\
  2354. Default name to DEFAULT if user enters a null string.\n\
  2355. Fourth arg MUSTMATCH non-nil means require existing file's name.\n\
  2356.  Non-nil and non-t means also require confirmation after completion.\n\
  2357. DIR defaults to current buffer's directory default.")
  2358.   (prompt, dir, defalt, mustmatch)
  2359.      Lisp_Object prompt, dir, defalt, mustmatch;
  2360. {
  2361.   Lisp_Object val, insdef, tem;
  2362.   struct gcpro gcpro1, gcpro2;
  2363.   register char *homedir;
  2364.   int count;
  2365.  
  2366.   if (NULL (dir))
  2367.     dir = current_buffer->directory;
  2368.   if (NULL (defalt))
  2369.     defalt = current_buffer->filename;
  2370.  
  2371.   /* If dir starts with user's homedir, change that to ~. */
  2372.   homedir = (char *) egetenv ("HOME");
  2373.   if (homedir != 0
  2374.       && XTYPE (dir) == Lisp_String
  2375.       && !strncmp (homedir, XSTRING (dir)->data, strlen (homedir))
  2376.       && XSTRING (dir)->data[strlen (homedir)] == '/')
  2377.     {
  2378.       dir = make_string (XSTRING (dir)->data + strlen (homedir) - 1,
  2379.              XSTRING (dir)->size - strlen (homedir) + 1);
  2380.       XSTRING (dir)->data[0] = '~';
  2381.     }
  2382.  
  2383.   if (insert_default_directory)
  2384.     insdef = dir;
  2385.   else
  2386.     insdef = build_string ("");
  2387.  
  2388. #ifdef VMS
  2389.   count = specpdl_ptr - specpdl;
  2390.   specbind (intern ("completion-ignore-case"), Qt);
  2391. #endif
  2392.  
  2393.   GCPRO2 (insdef, defalt);
  2394.   val = Fcompleting_read (prompt, intern ("read-file-name-internal"),
  2395.               dir, mustmatch,
  2396.               insert_default_directory ? insdef : Qnil);
  2397.  
  2398. #ifdef VMS
  2399.   unbind_to (count);
  2400. #endif
  2401.  
  2402.   UNGCPRO;
  2403.   if (NULL (val))
  2404.     error ("No file name specified");
  2405.   tem = Fstring_equal (val, insdef);
  2406.   if (!NULL (tem) && !NULL (defalt))
  2407.     return defalt;
  2408.   return Fsubstitute_in_file_name (val);
  2409. }
  2410.  
  2411. syms_of_fileio ()
  2412. {
  2413.   Qfile_error = intern ("file-error");
  2414.   staticpro (&Qfile_error);
  2415.   Qfile_already_exists = intern("file-already-exists");
  2416.   staticpro (&Qfile_already_exists);
  2417.  
  2418.   Fput (Qfile_error, Qerror_conditions,
  2419.     Fcons (Qfile_error, Fcons (Qerror, Qnil)));
  2420.   Fput (Qfile_error, Qerror_message,
  2421.     build_string ("File error"));
  2422.  
  2423.   Fput (Qfile_already_exists, Qerror_conditions,
  2424.     Fcons (Qfile_already_exists,
  2425.            Fcons (Qfile_error, Fcons (Qerror, Qnil))));
  2426.   Fput (Qfile_already_exists, Qerror_message,
  2427.     build_string ("File already exists"));
  2428.  
  2429.   DEFVAR_BOOL ("insert-default-directory", &insert_default_directory,
  2430.     "*Non-nil means when reading a filename start with default dir in minibuffer.");
  2431.   insert_default_directory = 1;
  2432.  
  2433.   DEFVAR_BOOL ("vms-stmlf-recfm", &vms_stmlf_recfm,
  2434.     "*Non-nil means write new files with record format `stmlf'.\n\
  2435. nil means use format `var'.  This variable is meaningful only on VMS.");
  2436.   vms_stmlf_recfm = 0;
  2437.  
  2438.   defsubr (&Sfile_name_directory);
  2439.   defsubr (&Sfile_name_nondirectory);
  2440.   defsubr (&Sfile_name_as_directory);
  2441.   defsubr (&Sdirectory_file_name);
  2442.   defsubr (&Smake_temp_name);
  2443.   defsubr (&Sexpand_file_name);
  2444.   defsubr (&Ssubstitute_in_file_name);
  2445.   defsubr (&Scopy_file);
  2446.   defsubr (&Sdelete_file);
  2447.   defsubr (&Srename_file);
  2448.   defsubr (&Sadd_name_to_file);
  2449. #ifdef S_IFLNK
  2450.   defsubr (&Smake_symbolic_link);
  2451. #endif /* S_IFLNK */
  2452. #ifdef VMS
  2453.   defsubr (&Sdefine_logical_name);
  2454. #endif /* VMS */
  2455. #ifdef HPUX_NET
  2456.   defsubr (&Ssysnetunam);
  2457. #endif /* HPUX_NET */
  2458.   defsubr (&Sfile_name_absolute_p);
  2459.   defsubr (&Sfile_exists_p);
  2460.   defsubr (&Sfile_readable_p);
  2461.   defsubr (&Sfile_writable_p);
  2462.   defsubr (&Sfile_symlink_p);
  2463.   defsubr (&Sfile_directory_p);
  2464.   defsubr (&Sfile_modes);
  2465.   defsubr (&Sset_file_modes);
  2466.   defsubr (&Sfile_newer_than_file_p);
  2467.   defsubr (&Sinsert_file_contents);
  2468.   defsubr (&Swrite_region);
  2469.   defsubr (&Sverify_visited_file_modtime);
  2470.   defsubr (&Sclear_visited_file_modtime);
  2471.   defsubr (&Sdo_auto_save);
  2472.   defsubr (&Sset_buffer_auto_saved);
  2473.   defsubr (&Srecent_auto_save_p);
  2474.  
  2475.   defsubr (&Sread_file_name_internal);
  2476.   defsubr (&Sread_file_name);
  2477. }
  2478.